//点赞功能
var p;
function initLike(srcEle,user,typeId,type) {
    //当我们登录的时候,先要看下我们也没有对这条游记进行了点赞,如果有,那么我们就要
    if (user){
        $.get("/praises",{userId: user.id, typeId: typeId, type: type},function (data) {
            if (data.success){
                p = data.data;
                if (p.state==1){
                    $(srcEle).attr("class", "fa fa-thumbs-up");
                }
            }
        })
    }
}

//点击点赞按钮
function like(srcEle,user,praise,typeId,type,pn,num) {
    if (!user) {
        $(document).dialog({
            type: "confirm",
            style: "IOS",
            titleShow: false,
            content: "请登录",
            buttons: [{
                name: "跳转到登录界面",
                callback: function () {
                    location.href = "/login.html";
                }
            }]
        });
        return;
    }
    //如果是第一次点赞,那么我们就增加一条记录
    if (!praise) {
        $.post("/praises", {userId: user.id, typeId: typeId, type: type, state: 1}, function (data) {
            praise = data.data;
            $(srcEle).attr("class", "fa fa-thumbs-up")
        })
    }
    //如果是切换,那么我们换下状态
    if (praise) {
        //取消赞
        if (praise.state == 1) {
            praise.state = 0;
            $.post("/praises", praise, function (data) {
                praise = data.data;
                $(srcEle).attr("class", "fa fa-thumbs-o-up")
                num--;
                $(pn).html(num);
                sessionStorage.setItem("praiseNum",num);
            })
        } else {
            //点赞
            praise.state = 1;
            $.post("/praises", praise, function (data) {
                praise = data.data;
                $(srcEle).attr("class", "fa fa-thumbs-up")
                num++;
                $(pn).html(num);
                sessionStorage.setItem("praiseNum",num);
            })
        }
    }
}